<template>
  <div class="mi-home">
    <!-- 左侧导航菜单 -->
    <div class="side-menu">
      <ul class="menu-list">
        <li v-for="item in menuItems" :key="item.id" class="menu-item">
          <span>{{ item.name }}</span>
          <el-icon><ArrowRight /></el-icon>

          <!-- 子菜单浮层 -->
          <div class="sub-menu">
            <div
              v-for="subItem in item.children"
              :key="subItem.id"
              class="sub-item"
              @click="goToGoods(subItem.id)"
            >
              <img :src="subItem.picture" :alt="subItem.name" />
              <span>{{ subItem.name }}</span>
            </div>
          </div>
        </li>
      </ul>
    </div>

    <!-- 主轮播区域 -->
    <div class="main-carousel">
      <el-carousel height="460px" :interval="4000" arrow="always">
        <el-carousel-item v-for="item in carouselItems" :key="item.id">
          <div
            class="carousel-content"
            :style="{ background: item.background }"
          >
            <div class="carousel-info">
              <div class="new-tag">{{ item.tag }}</div>
              <h2 class="product-name">{{ item.title }}</h2>
              <p class="product-desc">{{ item.description }}</p>
              <el-button class="action-button">{{ item.buttonText }}</el-button>
            </div>
            <div class="product-image">
              <img :src="item.image" :alt="item.title" />
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>
  </div>

  <!-- 服务和产品展示区域 -->
  <div class="service-product-section">
    <div class="service-grid">
      <div
        v-for="service in serviceItems"
        :key="service.id"
        class="service-item"
      >
        <i :class="service.icon"></i>
        <span>{{ service.title }}</span>
      </div>
    </div>

    <div class="product-showcase">
      <div 
        v-for="item in showcaseItems" 
        :key="item.id"
        class="showcase-item"
      >
        <img :src="item.image" :alt="item.title" />
        <div class="item-title">{{ item.title }}</div>
        <div class="item-desc">{{ item.description }}</div>
      </div>
    </div>
  </div>

  <!-- 产品列表区域 -->
  <div class="products-section">
    <div class="section-header">
      <h2>生鲜果蔬</h2>
      <div class="header-right">
        <a href="#" class="active">水果</a>
        <a href="#">蔬菜</a>
        <a href="#" class="more" @click.prevent="goToGoodsList">
          更多商品
          <el-icon><ArrowRight /></el-icon>
        </a>
      </div>
    </div>

    <div class="products-grid">
      <div class="product-item large">
        <img src="http://127.0.0.1:8888/static/image/category/foods/tomato.jpg" alt="Xiaomi Watch S4" />
        <h3>新鲜水果</h3>
        <p>出手今日</p>
      </div>

      <div 
        v-for="product in products" 
        :key="product.id"
        class="product-item"
      >
        <img :src="product.picture" :alt="product.name" />
        <h3>{{ product.name }}</h3>
        <p>{{ product.description }}</p>
        <div class="price">{{ product.price }}元</div>
        <div v-if="product.originalPrice" class="original-price">
          {{ product.originalPrice }}元
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>import { ref, onMounted } from "vue";
import { ArrowRight } from "@element-plus/icons-vue";
import { getCategoryList, getGoodsList } from "../../api/api";
import config from "../../config";
import { useRouter } from 'vue-router';

const router = useRouter();

// 左侧菜单数据
const menuItems = ref([]);
const arrayToTree = (items) => {
    const result = [];
    const itemMap = {};
    for (const item of items) {
        itemMap[item.id] = { ...item, children: [] };
    }

    for (const item of items) {
        const treeItem = itemMap[item.id];

        if (item.pid === 0) {
            result.push(treeItem);
        } else {
            const parent = itemMap[item.pid];
            parent.children.push(treeItem);
        }
    }
    return result;
};

onMounted(() => {
    loadCategoryList();
    loadProductList();
});

const loadCategoryList = async () => {
    const data = await getCategoryList();
    const baseURL = config.baseURL;
    const processedData = data.map(item => ({
        ...item,
        picture: item.picture ? `${baseURL}${item.picture}` : ''
    }));
    console.log("Processed Data");
    console.log(processedData);
    menuItems.value = arrayToTree(processedData);
};

const goToGoods = (id) => {
  router.push(`/goods/${id}`);
};

const goToGoodsList = () => {
  router.push('/good');
};
// 轮播图数据
const carouselItems = ref([
  {
    id: 1,
    tag: "热门产品",
    title: "小米笔记本 Pro 14",
    description: "小米笔记本 Pro 14 | 高性能轻薄本",
    buttonText: "立即查看",
    image: "http://127.0.0.1:8888/static/image/category/phone/computer.png"
  },
  {
    id: 2,
    tag: "最新产品",
    title: "青花瓷水杯",
    description: "青花瓷水杯 | 高端大气",
    buttonText: "立即查看",
    image: "http://127.0.0.1:8888/static/image/category/store/cup.png"
  },
]);

// 服务项数据
const serviceItems = ref([
  { id: 1, title: "官方服务", icon: "icon-service" },
  { id: 2, title: "企业团购", icon: "icon-enterprise" },
  { id: 3, title: "F码通道", icon: "icon-fcode" },
  { id: 4, title: "米粉卡", icon: "icon-card" },
  { id: 5, title: "以旧换新", icon: "icon-exchange" },
  { id: 6, title: "话费充值", icon: "icon-recharge" },
  { id: 7, title: "官方服务", icon: "icon-service" },
  { id: 8, title: "企业团购", icon: "icon-enterprise" },
  { id: 9, title: "F码通道", icon: "icon-fcode" },
]);

// 展示项数据
const showcaseItems = ref([
  {
    id: 1,
    title: "潮流女装",
    description: "潮流女装 | 时尚潮流",
    image: "http://127.0.0.1:8888/static/image/category/clothes/dress.png",
  },
  {
    id: 2,
    title: "食品",
    description: "食品 | 新鲜美味",
    image: "http://127.0.0.1:8888/static/image/category/foods/biscuit.jpg",
  },
  {
    id: 3,
    title: "珠宝饰品",
    description: "珠宝饰品 | 精美绝伦",
    image: "http://127.0.0.1:8888/static/image/category/jewelry/ornaments.jpg",
  },
]);

// 产品数据
const products = ref([]);

const loadProductList = async () => {
    try {
        const data = await getGoodsList();
        const baseURL = config.baseURL;
        const processedData = data.map(item => ({
            ...item,
            picture: item.picture ? `${baseURL}${item.picture}` : ''
        }));
        console.log("Processed Product Data");
        console.log(processedData);
        products.value = processedData.slice(0, 8); // 只显示前八个产品
    } catch (error) {
        console.error('获取产品列表失败:', error);
    }
};
</script>

<style scoped>
.mi-home {
  position: relative;
  width: 100%;
  max-width: 1226px;
  margin: 0 auto;
}

.side-menu {
  position: absolute;
  left: 0;
  top: 0;
  width: 234px;
  height: 460px;
  background: rgba(0, 0, 0, 0.6);
  z-index: 10;
}

.menu-list {
  padding: 20px 0;
  margin: 0;
  list-style: none;
}

.menu-item {
  padding: 0 24px 0 30px;
  height: 42px;
  line-height: 42px;
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}

.menu-item:hover {
  background: #ff6700;
}

.menu-item:hover .sub-menu {
  display: block;
}

.sub-menu {
  display: none;
  position: absolute;
  left: 234px;
  top: 0;
  height: 420px;
  background: #fff;
  border: 1px solid #e0e0e0;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.18);
  padding: 20px;
  min-width: 500px;
}

.sub-item {
  width: 25%;
  float: left;
  padding: 10px;
  text-align: center;
}

.sub-item img {
  width: 80px;
  height: 80px;
}

.sub-item span {
  display: block;
  color: #333;
  font-size: 14px;
}

.main-carousel {
  width: 1226px;
  margin: 0 auto;
  position: relative;
}

.main-carousel:hover :deep(.el-carousel__arrow) {
  opacity: 1;
}

.main-carousel:hover :deep(.el-carousel__arrow--left):hover,
.main-carousel:hover :deep(.el-carousel__arrow--right):hover {
  background-color: rgba(0, 0, 0, 0.15);
}

.carousel-content {
  display: flex;
  height: 100%;
  padding: 0 234px 0 300px;
}

.carousel-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  color: rgb(89, 181, 204);
}

.new-tag {
  font-size: 18px;
  margin-bottom: 20px;
}

.product-name {
  font-size: 36px;
  margin-bottom: 15px;
}

.product-desc {
  font-size: 16px;
  margin-bottom: 30px;
}

.action-button {
  width: 120px;
  background: #ff6700;
  border: none;
  color: #fff;
}

.action-button:hover {
  background: #f25807;
}

.product-image {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.product-image img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

:deep(.el-carousel__arrow) {
  border-radius: 0;
  width: 40px;
  height: 70px;
  opacity: 0;
  transition: opacity 0.3s ease;
}

:deep(.el-carousel__arrow--left) {
  left: 234px;
}

:deep(.el-carousel__arrow--right) {
  right: 0;
}

:deep(.el-carousel__arrow:hover) {
  background-color: rgba(0, 0, 0, 0.25);
}

.service-product-section {
  display: flex;
  gap: 15px;
  max-width: 1226px;
  margin: 0 auto;
  padding: 20px 0;
}

.service-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1px;
  background: #fff;
  width: 234px;
  height: fit-content;
}

.service-item {
  height: 112px;
  background: #5f5750;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
  opacity: 0.7;
  transition: opacity 0.3s;
  cursor: pointer;
}

.service-item:hover {
  opacity: 1;
}

.service-item i {
  font-size: 24px;
  margin-bottom: 4px;
}

.service-item span {
  font-size: 12px;
}

.product-showcase {
  display: flex;
  flex: 1;
  gap: 15px;
}

.showcase-item {
  flex: 1;
  background: #fff;
  transition: all 0.3s ease;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.showcase-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.showcase-item img {
  width: 100%;
  height: 160px;
  object-fit: cover;
  margin-bottom: 15px;
}

.item-title {
  text-align: center;
  padding: 0;
  font-size: 14px;
  color: #333;
  margin-bottom: 5px;
  font-weight: 500;
}

.item-desc {
  text-align: center;
  padding: 0;
  font-size: 12px;
  color: #5f5750;
  margin-bottom: 10px;
}

.products-section {
  max-width: 1226px;
  margin: 0 auto;
  padding: 20px 0;
  margin-bottom: 150px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-header h2 {
  font-size: 22px;
  font-weight: 200;
  color: #333;
}

.header-right {
  display: flex;
  gap: 20px;
  align-items: center;
}

.header-right a {
  color: #757575;
  text-decoration: none;
  font-size: 16px;
}

.header-right a.active {
  color: #ff6700;
}

.header-right .more {
  color: #ff6700;
  display: flex;
  align-items: center;
  gap: 4px;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 15px;
}

.product-item {
  background: #fff;
  padding: 20px;
  text-align: center;
  transition: all 0.3s ease;
  cursor: pointer;
}

.product-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
}

.product-item.large {
  grid-column: span 1;
  grid-row: span 2;
  background: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.product-item img {
  width: 160px;
  height: 160px;
  object-fit: contain;
  margin-bottom: 10px;
}

.product-item.large img {
  width: 80%;
  height: auto;
  margin-bottom: 20px;
}

.product-item h3 {
  font-size: 14px;
  color: #333;
  margin-bottom: 5px;
}

.product-item p {
  font-size: 12px;
  color: #757575;
  margin-bottom: 10px;
}

.price {
  color: #ff6700;
  font-size: 14px;
  font-weight: bold;
}

.original-price {
  color: #b0b0b0;
  font-size: 12px;
  text-decoration: line-through;
  margin-top: 2px;
}
</style>